package com.zjitc.controller;

import com.zjitc.common.ServerResponse;
import com.zjitc.pojo.User;
import com.zjitc.vo.Msg;
import com.zjitc.vo.PageModel;
import com.zjitc.service.ProductService;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Create By IntelliJ IDEA
 *
 * @author: myq
 * @create-Time: 2017/11/15  14:51
 */
@Controller
@RequestMapping("/product")
public class ProductController {

  private final int COUNT = 12;

  @Autowired
  private ProductService service;

  @RequestMapping(value = "/displaymsg", produces = "application/json;charset=UTF-8")
  public ModelAndView displaymsg(HttpSession session, String pid)
      throws ServletException, IOException {
    return service.displaymsg(session, pid);
  }

  @RequestMapping("/list")
  public ModelAndView list(@RequestParam("cid") String cid,
      @RequestParam(value = "pageid", required = false, defaultValue = "1") Integer pageid)
      throws ServletException, IOException {
    PageModel info = service.findall(cid, pageid, COUNT);
    ModelAndView mv = new ModelAndView();
    mv.addObject("pagemodel", info);
    mv.addObject("cid", cid);
    mv.setViewName("jsp/product_list");
    return mv;
  }


  @RequestMapping("delhistory")
  @ResponseBody
  public ServerResponse delHistory(HttpSession session){
    try{
      String history = "history";
      session.removeAttribute(history);
      return ServerResponse.createSuccess();
    }catch (Exception e){
      e.printStackTrace();
      return ServerResponse.createError();
    }
  }

  @RequestMapping(value = "/fuzzysearch",method = RequestMethod.POST)
  public ModelAndView fuzzy(String fuzzy,
      @RequestParam(value = "pageid", required = false, defaultValue = "1") int pageid) {
    PageModel model = service.fuzzysearch(fuzzy, COUNT, pageid);
    ModelAndView m = new ModelAndView();
    m.addObject("pagemodel", model);
    m.addObject("fuzzy", fuzzy);
    m.setViewName("jsp/fuzzy_list");
    return m;
  }

  @RequestMapping(value = "updateCollect",method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse addCollect(String pid, HttpSession session, HttpServletRequest request) {
    User user = (User) session.getAttribute("user");
    if (user == null) {
      String msg = "未检测到用户，请先登录";
      String address = request.getContextPath() + "jsp/login";
      Msg ms = new Msg(msg, address);
      return ServerResponse.createError(ms);
    }
    return service.updateCollect(pid, user);
  }

//  ----------------------------------评论--------------------------------------


}
